#ifndef XML_PARSER_H
#define XML_PARSER_H

#include "cocos2d.h"
#include <libxml/parser.h>
#include <libxml/tree.h>


cocos2d::CCRect convertToRect(const std::string& str);
cocos2d::CCPoint convertToPoint(const std::string& str);

class XmlParser
{
public:
	XmlParser(const char* filename = 0);
	~XmlParser();

	// get value methods
	bool	getBoolForKey(const char* pKey, int index = 0, bool defaultValue = false);
	int		getIntegerForKey(const char* pKey, int index = 0, int defautValue = 0);
	std::vector<int> getIntArrayForKey(const char* pKey, int index = 0);
	std::vector<float> getFloatArrayForKey(const char* pKey, int index = 0);
	float	getFloatForKey(const char* pKey, int index = 0, float defautValue = 0.0f);
	double  getDoubleForKey(const char* pKey, int index = 0, double defautValue = 0.0);
	cocos2d::CCRect getRectForKey(const char* pKey, int index = 0, const cocos2d::CCRect& defaultRect=cocos2d::CCRect(0,0,0,0));
	cocos2d::CCPoint getPointForKey(const char* pKey, int index = 0, const cocos2d::CCPoint& defaultPoint=cocos2d::CCPoint(0,0));
	cocos2d::CCSize getSizeForKey(const char* pKey, int index = 0, const cocos2d::CCSize& defaultSize=cocos2d::CCSize(0,0));
	std::string getStringForKey(const char* pKey, int index = 0, const char* defaultValue = "");

	bool	gotoNextGroup();
	bool	gotoChildGroup();
	std::string curNodeName();

	std::vector<std::string> allPropertyNames();
	// set value methods
	void	setBoolForKey(const char* pKey, bool value, int index = 0);
	void	setIntegerForKey(const char* pKey, int value, int index = 0);
	void	setFloatForKey(const char* pKey, float value, int index = 0);
	void	setDoubleForKey(const char* pKey, double value, int index = 0);
	void	setStringForKey(const char* pKey, std::string value, int index = 0);

	void	setAtrribute(const char* pKey, const char* value);
	std::string getAttribute(const char* pKey);
	int		getIntegerForAttr(const char* pKey, int defaultValue = 0);
	cocos2d::CCPoint getPointForAttr(const char* pKey, const cocos2d::CCPoint& defaultPoint=cocos2d::CCPoint(0,0));
	cocos2d::CCRect	 getRectForAttr(const char* pKey, const cocos2d::CCRect& defaultRect=cocos2d::CCRect(0,0,0,0));
	cocos2d::CCSize getSizeForAttr(const char* pKey, const cocos2d::CCSize& defaultSize=cocos2d::CCSize(0,0));
	cocos2d::ccColor4B getColorForAttr(const char* pKey, const cocos2d::ccColor4B& defaultColor=cocos2d::ccc4(255,255,255,255));
	double	getDoubleForAttr(const char* pKey, double defaultValue=0.0);
	float	getFloatForAttr(const char* pKey, float defaultValue=0.0);



	bool	beginGroup(const char* groupName, int index = 0);
	void	endGroup();
	bool initXMLFile(const char* filename);
	void saveXMLFile();
	void reset();
public:

	static bool isXMLFileExist(const char* filename);
	std::string& getFileName() { return mFilename; } 

	bool	newGroup(const char* groupName);
    static bool createXMLFile(const char* newFileName);

	
/**
 * define the functions here because we don't want to
 * export xmlNodePtr and other types in "CCUserDefault.h"
 */

xmlNodePtr getXMLNodeForKey(const char* pKey, xmlNodePtr parentNode, int index = 0);

std::string getValueForKey(const char* pKey, int index = 0);
void setValueForKey(const char* pKey, const char* pValue, int index = 0);

private:
	xmlDocPtr	mDoc;
	xmlNodePtr	mCurNode;
	std::string mFilename;
};


#endif